﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web.Mvc;
using FarPoint.Win.Spread;
using Workflow.Common.ExcelHelp;
using Workflow.Common.Tools;
using Workflow.Dto;
using Workflow.IService;
using Workflow.Resource;
using Workflow.Web.Common;

namespace Workflow.Web.Controllers
{
	[Localization]
	[UserAuthorize]
	public class ReportController : Controller
	{
		// GET: /Report/
		public ICaseEEMService   CaseEEMService   { get; set; }
		
		public ActionResult EEMReport()
		{
			string groupCode = Session[ConfigReader.AuthorizeSessionUserGroupCode].ToString();
			ViewData["GroupCode"] = int.Parse(groupCode);
			return View();
		}

		

		#region  EEM
		public ActionResult ShowEEMReport()
		{
			try
			{
				var from = Request["DateFrom"];
				var to = Request["DateTo"];
				var reportType = Request["ReportType"];
				var folderLevelA = Request["FolderLevelA"];
				var folderLevelB = Request["FolderLevelB"];
				//var batchNo = Request["BatchNo"];

				ReceivedCaseDayReportQueryDto reportPtpQueryDto = new ReceivedCaseDayReportQueryDto();

				reportPtpQueryDto.FromDay = from;
				reportPtpQueryDto.ToDay = to;
				reportPtpQueryDto.FolderLevelA = folderLevelA;
				reportPtpQueryDto.FolderLevelB = folderLevelB;

				//reportPtpQueryDto.BatchNo = batchNo;

				GetReportTemplate(reportType, reportPtpQueryDto);

				return Json(new { Success = true, Message = Global.ActiveSuccess }, "text/html", JsonRequestBehavior.AllowGet);
			}
			catch (Exception ex)
			{
				var debug = ConfigurationManager.AppSettings["DebugFlag"];
				if (debug == "on")
				{
					return Json(new { Success = false, Message = ex.Message }, "text/html", JsonRequestBehavior.AllowGet);

				}
				else
				{
					return Json(new { Success = false, Message = Global.ActiveFalse }, "text/html", JsonRequestBehavior.AllowGet);
				}
			}
			finally
			{
				//System.GC.Collect();
			}

		}

		private void GetReportTemplate(string type, ReceivedCaseDayReportQueryDto reportPtpQueryDto)
		{

			switch (type)
			{

				case "1":
					{
						//CycleTimeReportEEM(reportPtpQueryDto);
						//CycleTimeReportEEMNew(reportPtpQueryDto);
						break;
					}
				case "2":
					{
						FirstPassYeildReportEEM(reportPtpQueryDto);
						break;
					}
				case "3":
					{
						//ReceivedCaseDayReportEEM(reportPtpQueryDto);
						break;
					}
				case "4":
					{
						//ReceivedCaseMonthReportEEM(reportPtpQueryDto);
						break;
					}
				case "5":
					{
						//UserWorkloadReportEEM(reportPtpQueryDto);
						break;
					}
				case "6":
					{
						//ReasonReportEEM(reportPtpQueryDto);
						break;
					}
			}

		}

		private void FirstPassYeildReportEEM(ReceivedCaseDayReportQueryDto reportPtpQueryDto)
		{
			
			try
			{
				IList<FirstPassYeildReportEEMDto> firstPassYeildReportReportEemDtos = CaseEEMService.GetFirstPassYeildReportEEM(reportPtpQueryDto);

				string path = Server.MapPath("~/ReportTemplate/FirstPassYeildEEM.xls");
				string pathTemp = Server.MapPath("~/TempFiles/" + ToolsHelper.GetTimeZoneNow().ToString("yyyyMMddhhmmss") + ".xls");
				//ExcelEdit edit = new ExcelEdit();


				FpSpread spread = new FpSpread();
				spread.OpenExcel(path);
				SheetView sheetView = spread.Sheets[0];
				
			
				int count = 0;
				for (int i = 0; i < firstPassYeildReportReportEemDtos.Count; i++)
				{
					//具体报表插入数据逻辑
					sheetView.Cells[3 + count, 1].Value = firstPassYeildReportReportEemDtos[i].DayDate;
					

					count += 3;
				}
			
				spread.SaveExcel(pathTemp);
				spread.Dispose();

				var file = new FileInfo(pathTemp);
				Response.Clear();
				Response.Buffer = true;
				Response.Charset = "";
				Response.AppendHeader("Content-Disposition",
									  "attachment;filename=" +
									  Server.UrlEncode(ToolsHelper.GetTimeZoneNow().ToString("yyyyMMddhhmmss") + ".xls"));
				//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文

				Response.WriteFile(file.FullName);
				Response.End();
				System.IO.File.Delete(pathTemp);
			}
			catch (Exception ex)
			{

				throw ex;
			}
			finally
			{
				//

			}
		}

		
		#endregion

		
	}
}
